home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cenvid.zip / DELTREE.BAT < prev    next >
DOS Batch File  |  1993-06-25  |  5KB  |  111 lines

  1. @echo off
  2. REM *******************************************************************
  3. REM *** DelTree - Delete a subdirectory and all files in it, and    ***
  4. REM ***           recursively delete all subdirectories within that ***
  5. REM ***           subdirectory.  You may look at DelTree1.bat and   ***
  6. REM ***           DelTree2.bat for other methods of doing the       ***
  7. REM ***           same thing by using temporary files.              ***
  8. REM *******************************************************************
  9. if "%1"=="" GOTO SHOW_HOW
  10.  
  11. REM *******************************************************************
  12. REM *** The user could be very unhappy to delete lots of files they ***
  13. REM *** didn't really want to delete, and so give them a chance to  ***
  14. REM *** change their mind.                                          ***
  15. REM *******************************************************************
  16.    ECHO THIS WILL DELETE ALL FILES AND DIRECTORIES UNDER %1
  17.    CEnvi GetUKey.cmm ARE YOU SURE YOU WANT TO DO THIS? (Y/N)    yn
  18.    if N==%UKEY% GOTO FINI
  19.  
  20. REM *********************************************************************
  21. REM *** Check that this is a valid subdirectory.  This can be handled ***
  22. REM *** by calling another CEnvi program: ValidDir.bat                ***
  23. REM *********************************************************************
  24.    CEnvi ValidDir.bat %1 COMPLAIN
  25.    if ERRORLEVEL 1 GOTO FINI
  26.  
  27. REM ******************************************************************
  28. REM *** It will be simpler to delete files if we make sure that no ***
  29. REM *** attributes are set that would make them undeletable.  The  ***
  30. REM *** easy way to change this is to let the DOS attrib function  ***
  31. REM *** clear all of these attributes for us.                      ***
  32. REM ******************************************************************
  33.    attrib -H -S -R %1\*.* /s > NUL
  34.  
  35. REM **************************************************************************
  36. REM *** Temporarily turn off DELDIR to make this deletion fast (if unsafe) ***
  37. REM **************************************************************************
  38. set DELTREE_SAVE_DEL_DIR=%DELDIR%
  39. set DELDIR=
  40.  
  41. REM ******************************************************************
  42. REM *** The rest of this task will be handled by CEnvi, which will ***
  43. REM *** create a list of all directories and then call the command ***
  44. REM *** shell to delete the files in each directory.               ***
  45. REM ******************************************************************
  46. CEnvi %0.bat %1
  47. GOTO CENVI_EXIT
  48.  
  49.    main(argc,argv)
  50.    {
  51.       // build a sorted list of all subdirectories under the specified directory
  52.       DirCount = BuildSubdirList(argv[1],DirList)
  53.       if ( DirCount != 0 ) {
  54.          // sort the subdir list in reverse alphabetic order, which insures that
  55.          // subdirectoies of a directory appear before its parent directory
  56.          qsort(DirList,DirCount,"ReverseSortDirnames")
  57.          // for each subdirectory, call command shell to delete all files in it
  58.          // and then remove the directory
  59.          for ( i = 0; i < DirCount; i++ ) {
  60.             DeleteAllFilesInDir( DirList[i].name );
  61.             RemoveDirectory( DirList[i].name );
  62.          }
  63.       }
  64.       // all subdirectories have have been deleted, then delete the input subdir
  65.       DeleteAllFilesInDir( argv[1] );
  66.       RemoveDirectory( argv[1] );
  67.    }
  68.  
  69.    BuildSubdirList(BaseDir,List) // build List of all subdirectories under BaseDir
  70.    {                             // return count of elements in list
  71.       sprintf(SearchSpec,"%s\\*.*",BaseDir)
  72.       List = Directory(SearchSpec,TRUE,
  73.                        FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  74.                        FATTR_SUBDIR)
  75.       return( ( List == NULL ) ? 0 : 1+GetArraySpan(List) );
  76.    }
  77.  
  78.    ReverseSortDirnames(Dir1,Dir2) // repeatedly called by qsort to sort Dir1 and Dir2
  79.    {
  80.       return( stricmp(Dir2.name,Dir1.name) );
  81.    }
  82.  
  83.    DeleteAllFilesInDir(DirName) // call system to delete all files in this dir
  84.    {
  85.       printf("DEL %s\n",DirName)
  86.       system("echo Y | del %s > NUL",DirName)
  87.    }
  88.  
  89.    RemoveDirectory(DirName) // call system RMDIR call
  90.    {
  91.       printf("RMDIR %s\n",DirName)
  92.       system("RMDIR %s > NUL",DirName)
  93.    }
  94.  
  95. :CENVI_EXIT
  96. REM **********************
  97. REM *** Restore DELDIR ***
  98. REM **********************
  99. set DELDIR=%DELTREE_SAVE_DEL_DIR%
  100. set DELTREE_SAVE_DEL_DIR=
  101. GOTO FINI
  102.  
  103. :SHOW_HOW
  104. ECHO  
  105. ECHO DelTree.bat - Delete a subdirectory and all files and directories under it
  106. ECHO  
  107. GOTO FINI
  108.  
  109. :FINI
  110. set UKEY=
  111.